//
//  VideoViewController.m
//  Delicious2
//
//  Created by FSD on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "VideoViewController.h"
#import "MJChiBaoZiHeader.h"
#import "MJChiBaoZiFooter.h"
@interface VideoViewController ()<UITableViewDataSource, UITableViewDelegate, PicPushWebViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *topDataSource;
@property (nonatomic, strong) NSMutableArray *dataDataSource;
@property (nonatomic, strong) NSMutableArray *picUrlDataSource;
@property (nonatomic, strong) NSMutableArray *titleDataSource;
@property (nonatomic, strong) NSMutableArray *detailDataSource;
@property (nonatomic, strong) MBProgressHUD *progressHud;

@end

@implementation VideoViewController



#pragma mark - PicPushWebViewDelegate
- (void)pushNextViewController:(NSString *)strTitle withArrayId:(NSString *)strId withTopModel:(TopModel *)topModel {
    VideoDetailViewController *detail = [[VideoDetailViewController alloc] init];
    detail.urlStr = strId;
    detail.titleName = topModel.banner_title;
    detail.kind = @"食课";
    detail.topModel = topModel;
    [self.navigationController pushViewController:detail animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"食课";
    [self createView];
    self.progressHud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.progressHud.labelText = @"正在为您加载中...";
    self.progressHud.animationType = MBProgressHUDAnimationFade;
    self.tableView.mj_header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
//    [self.tableView.mj_header beginRefreshing];
    
    [self getData];
}


#pragma mark - 创建tableView
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[VideoTableViewCell class] forCellReuseIdentifier:@"VideoTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

- (void)getData {
    self.picUrlDataSource = [NSMutableArray arrayWithCapacity:0];
    self.titleDataSource = [NSMutableArray arrayWithCapacity:0];
    self.detailDataSource = [NSMutableArray arrayWithCapacity:0];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded; charset=utf-8" forKey:@"Content-Type"];
    [FSDNetWorkTool postWithURL:@"http://api.izhangchu.com/" body:@"methodName=CourseIndex&page=1&size=10&token=0&user_id=0&version=4.02" bodyType:BodyTypeString httpHeader:dic responseType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result[@"data"];
        VideoModel *videoModel = [[VideoModel alloc] initWithDictionary:dic];
        self.topDataSource = [NSMutableArray arrayWithArray:videoModel.topDataSource];
        self.dataDataSource = [NSMutableArray arrayWithArray:videoModel.dataDataSource];
        for (DataModel *dataModel in self.dataDataSource) {
            NSArray *array = [dataModel.series_name componentsSeparatedByString:@"#"];
            [self.titleDataSource addObject:array[1]];

        }
        for (TopModel *topModel in self.topDataSource) {
            [self.picUrlDataSource addObject:topModel.banner_picture];
            [self.detailDataSource addObject:topModel.banner_link];
        }
        if (self.dataDataSource.count > 0) {
            PhotoClass *photo = [[PhotoClass alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) withArray:self.picUrlDataSource withArrayTitle:self.titleDataSource withArrayId:self.detailDataSource withTopModelArray:self.topDataSource];
            photo.kind = @"食课";
            photo.label.hidden = YES;
            photo.delegate = self;
            [photo nightMode];
            self.tableView.tableHeaderView = photo;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.progressHud hide:YES];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)createScrollView {

}

#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoTableViewCellIdentifier"];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];
    if ([passWord isEqualToString:@"Night"]) {
        cell.backgroundColor = [UIColor grayColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
    //数组越界保护
    if (self.dataDataSource.count > indexPath.row) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataModel = self.dataDataSource[indexPath.row];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataDataSource.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    VideoDetailViewController *detail = [[VideoDetailViewController alloc] init];
    NSString *str = [NSString stringWithFormat:@"%@", [self.dataDataSource[indexPath.row] series_id]];
    detail.urlId = str;
    NSArray *nameArray = [[self.dataDataSource[indexPath.row] series_name] componentsSeparatedByString:@"#"];
    detail.titleName = nameArray[1];
    detail.kind = @"食课";
    detail.dataModel = self.dataDataSource[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 280;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.tableView nightMode];
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
